<?php

namespace app\api\controller;

use addons\shopro\service\commission\Agent;
use app\admin\model\shopro\commission\AgentUpLog;
use app\admin\model\shopro\goods\Goods;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\Refund;
use app\admin\model\shopro\user\User as UserModel;
use app\common\controller\Api;
use think\Log;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $user_id = 12160;

        $agentId = $user_id;

        $data = UserModel::where('parent_user_id', $agentId)
            ->where('status', 'normal')
            ->with(['agent' => function ($query) {
                return $query->with(['level_info','honor_info']);
            }])
            ->paginate($this->request->param('list_rows', 8));

        $this->success("", $data);
//        $order = Order::with('items')->where('id', $orderId)->find();
//        $uniqueGoodsIds = array_unique(array_column(objectToArray($order->items), 'goods_id'));
//        $goodsTypes = Goods::whereIn('id',$uniqueGoodsIds)->column('goods_type');
//        $agent = new Agent($user_id);
//        $nowLevel_single_consume = 0;
//        foreach ($goodsTypes as $goodsType) {
//            Log::info("商品类型{$goodsType}");
//
//            if ($goodsType === 'bucha') {
//                $now_level = $agent->getNowAgentLevel();
//
//                Log::info("用户当前等级金额{$now_level['upgrade_rules']['single_consume']}");
//
//                $nowLevel_single_consume = !empty($now_level['upgrade_rules']['single_consume']) ? $now_level['upgrade_rules']['single_consume'] : 0;
//            }
//        }
//
//        echo '<pre>';
////var_dump($nowLevel_single_consume);die();
//        \think\Queue::push('\addons\shopro\job\Commission@agentUpgrade', [
//            'user_id' => 2,
//            'order_id' => $orderId
//        ], 'shopro');
//        die();
//        $res = Order::with('items')->where('id', 35)->find();
//        echo '<pre>';
//        var_dump($res->items);
//        die();


//        var_dump(getLastMonthDate());die();
//        $user_id = 2;
//        $order_id = 4;
//        $agent = new Agent($user_id);
//        $now_level = $agent->getNowAgentLevel();
//        $nowLevel_single_consume = !empty($now_level['upgrade_rules']['single_consume']) ? $now_level['upgrade_rules']['single_consume'] : 0;
//        echo '<pre>';
//        var_dump($nowLevel_single_consume);die();
        //升级会员
//        $agent->getAgentStatus(true);
//        $agent->createAsyncAgentUpgrade();//会员等级升级
//        $agent->createAsyncAgentUpgradeHonor($user_id);//会员奖衔升级
//        $agent->runAgentUpgradeHonor();//会员奖衔升级
//        $agent->runAgentLevel5SharMoney();//每日金牌会员分佣
//        $agent->runMonthSharMoney();//每月1号销售提成

//        echo '<pre>';
//        var_dump('用户id'.$agent->user->id);
//        var_dump('奖衔'.$agent->agent->honor);
//        var_dump('等级'.$agent->agent->level);

//        AgentUpLog::upgradeUserUpLog($agent->user->id, 3, $agent->agent->honor);



//        $agent->runAgentUpgradeLevel($user_id,$order_id);

        $this->success('请求成功');
    }

    public function getConfig()
    {
        $re = Order::with('items')->where('id', '2')->find();
        var_dump($re);die();



        $key = $this->request->get('key');
        $content = config("site.{$key}");
        $this->success('success',$content);
    }
}
